package com.useeinfo.oa.open.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.DataCatBiz;
import com.useeinfo.oa.modules.base.biz.FacilityBiz;
import com.useeinfo.oa.modules.base.biz.FacilityDataBiz;
import com.useeinfo.oa.modules.base.entity.*;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @auther: bin.liu
 * @Date: 2018/12/24 10:51
 * @Description:
 */
@RestController
@RequestMapping("/api/facilityData")
public class FacilityDataAPI {

    @Autowired
    private FacilityDataBiz facilityDataBiz;

    @Autowired
    private FacilityBiz facilityBiz;

    @Autowired
    private DataCatBiz dataCatBiz;

    /**
     * 资料列表
     */
    @RequestMapping(value = "/getFacilityDataList")
    public ResultDto getFacilityList(@RequestParam(value = "pageIndex", required = false) String pageIndexParam,
                                     @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                     @RequestParam(value = "dataCatId", required = false) String dataCatId,
                                     @RequestParam(value = "facilityId", required = false) String facilityId,
                                     @RequestParam(value = "searchCode", required = false) String searchCode,
                                     HttpServletRequest request){
        QueryParam queryParam = new QueryParam(pageIndexParam, pageSizeParam);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if(enterprise!=null){
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId()+"");
        }
        if(StringUtils.isNotBlank(dataCatId)){
            queryParam.getSqlMap().put("dataCatId", dataCatId);
        }
        if(StringUtils.isNotBlank(facilityId)){
            queryParam.getSqlMap().put("facilityId", facilityId);
        }
        if(StringUtils.isNotBlank(searchCode)){
            queryParam.getSqlMap().put("searchCode", searchCode);
        }

//        List<FacilityData> facilityDataList =facilityDataBiz.findList(queryParam);
//        Long records = facilityDataBiz.totalRecord(queryParam.getSqlMap());
//        JSONObject result = new JSONObject();
//        JSONArray rows = new JSONArray();
//        for (FacilityData facilityData : facilityDataList){
//            JSONObject json = new JSONObject();
//            json.put("id",facilityData.getId());
//            json.put("serialNum",facilityData.getSerialNum());
//            json.put("facilityDataName", StringConverters.null2String(facilityData.getFacilityDataName()));
//            json.put("description", StringConverters.null2String(facilityData.getDescription()));
//            List<String> facilities = new ArrayList<>();
//            facilityData.getFacilitySet().forEach(facility -> {
//                facilities.add(facility.getName());
//            });
//            Collections.sort(facilities);
//            json.put("facilities", facilities);
//            List<String> dataCats = new ArrayList<>();
//            facilityData.getDataCatSet().forEach(dataCat -> {
//                dataCats.add(dataCat.getDataCatName());
//            });
//            Collections.sort(dataCats);
//            json.put("dataCats", dataCats);
//            String filePath = "";
//            String fileName = "";
//            if (facilityData.getResourceSet() != null && facilityData.getResourceSet().size() != 0) {
//                List<Resource> resourceList = new ArrayList<>(facilityData.getResourceSet());
//                String hostPath = Configuration.getConfigurationByName("hostUrl");
//                hostPath += hostPath.endsWith("/") ? "" : "/";
//                filePath = hostPath + resourceList.get(0).getPath();
//                fileName = resourceList.get(0).getResourceName();
//            }
//            json.put("filePath", filePath);
//            json.put("fileName", fileName);
//            rows.add(json);
//        }
//        result.put("rows", rows);
//        result.put("records", records);
        return ResultUtil.success(facilityDataBiz.findJSONList(queryParam));
    }

    /**
     * 获取设备资料详情
     */
    @RequestMapping(value = "/getFacilityDataDetail")
    public ResultDto getFacilityDetail(@RequestParam(value = "id", required = false) String id,
                                       HttpServletRequest request){
        FacilityData facilityData =facilityDataBiz.findModel(StringConverters.ToLong(id));
        if(facilityData==null){
            return  ResultUtil.error("无此资料");
        }
        JSONObject jsonObject = facilityData.toJSONObject();
        return ResultUtil.success(jsonObject);
    }

    /**
     * 获取设备列表
     */
    @RequestMapping(value = "/getFacilityList")
    public ResultDto getFacilityList(@RequestParam(value = "pageIndex", required = false) String pageIndexParam,
                                            @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                            HttpServletRequest request){
        QueryParam queryParam = new QueryParam(pageIndexParam, pageSizeParam);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if(enterprise!=null){
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId()+"");
        }
        List<Facility> facilityList =facilityBiz.findList(queryParam);
        JSONArray array = new JSONArray();
        for (Facility facility : facilityList){
            JSONObject json = new JSONObject();
            json.put("facilityName",facility.getName());
            json.put("facilityId",facility.getId());
            array.add(json);
        }
        return ResultUtil.success(array);
    }

    /**
     * 获取资料分类列表
     */
    @RequestMapping(value = "/getDataCatList")
    public ResultDto getDataCatList(@RequestParam(value = "pageIndex", required = false) String pageIndexParam,
                                            @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                            HttpServletRequest request){
        QueryParam queryParam = new QueryParam(pageIndexParam, pageSizeParam);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if(enterprise!=null){
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId()+"");
        }
        List<DataCat> dataCatList =dataCatBiz.findList(queryParam);
        JSONArray array = new JSONArray();
        for (DataCat dataCat : dataCatList){
            JSONObject json = new JSONObject();
            json.put("dataCatName",dataCat.getDataCatName());
            json.put("dataCatId",dataCat.getId());
            array.add(json);
        }
        return ResultUtil.success(array);
    }
}
